<?php

namespace app\common\model\banxi\subject;

use app\common\enums\SubjectStatusEnum;
use app\common\model\common\BaseModel;
use think\exception\ValidateException;
use think\model\concern\SoftDelete;

/**
 * Attachment模型
 * @property string url              文件物理路径
 * @property int quote            上传(引用)次数
 * @property int last_upload_time 最后上传时间
 */
class Subject extends BaseModel
{
    use SoftDelete;

    protected $name = 'banxi_subject';

    protected $autoWriteTimestamp = true;

    protected $deleteTime = 'del';

    protected $defaultSoftDelete = 0;

    protected $append = [
        'statusCn'
    ];

    protected $type = [
        'id' => 'string',
        'del' => 'string',
        'sortNum' => 'string',
        'createTime' => 'timestamp:Y-m-d H:i:s',
        'updateTime' => 'timestamp:Y-m-d H:i:s',
    ];


    //-----------------------------------------------------------------------------------
    //｜                                   获取器                                       ｜
    //-----------------------------------------------------------------------------------
    public function getStatusCnAttr($value, $data)
    {
        $value = $value ?: ($data['status'] ?? '');
        return (new SubjectStatusEnum())->getDescription($value) ?? '';
    }

    //-----------------------------------------------------------------------------------
    //｜                                   处理方法                                       ｜
    //-----------------------------------------------------------------------------------


    public function saveOrUpdate($data)
    {
        $id = $data['id'] ?? null;
        if ($id == null) {
            $gradeModel = new self();
            $gradeModel->save($data);
        } else {
            $gradeModel = self::where('id', $id)->find();
            if (empty($gradeModel)) {
                throw new ValidateException('科目不存在');
            }
            $gradeModel->save($data);
        }
        return true;
    }



    //-----------------------------------------------------------------------------------
    //｜                                   模型关联                                       ｜
    //-----------------------------------------------------------------------------------


}